<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/9/15 12:23
 */

namespace App\Dao\Users;


use App\Constants\System\SysStatusConst;
use App\Constants\User\UserAccountDefaultConst;
use App\Constants\User\UserAccountTypeConst;
use App\Dao\BaseDao;
use App\Model\Users\UsersAccount;

class UsersAccountDao extends BaseDao
{


    public function getInfoByPk(int $id)
    {
        return UsersAccount::query()->where('id','=',$id)
            ->where('is_del','=',SysStatusConst::NORMAL->value)
            ->first();
    }


    /**
     * 获取用户系统账户
     * @param int $uid
     * @return UsersAccount|\Hyperf\Database\Concerns\BuildsQueries|\Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
     */
    public function getSysAccountInfo(int $uid)
    {
        return UsersAccount::query()
            ->where('uid','=',$uid)
            ->where('type','=',UserAccountTypeConst::SYS->value)
            ->where('is_del','=',SysStatusConst::NORMAL->value)
            ->first();
    }


    /**
     * 获取默认账册
     * @param int $uid
     * @return UsersAccount|\Hyperf\Database\Concerns\BuildsQueries|\Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
     */
    public function getDefaultAccountInfo(int $uid)
    {
        return UsersAccount::query()
            ->where('uid','=',$uid)
            ->where('is_default','=',UserAccountDefaultConst::DEFAULT->value)
            ->where('is_del','=',SysStatusConst::NORMAL->value)
            ->first();
    }

    /**
     * 更新账户非默认
     * @param int $uid
     * @param int $accountId
     * @return void
     */
    public function updateAccountNotDefault(int $uid,int $accountId)
    {
        UsersAccount::query()->where('uid','=',$uid)
            ->where('is_del','=',SysStatusConst::NORMAL->value)
            ->whereNotIn('id',[$accountId])
            ->where('is_default','=',UserAccountDefaultConst::DEFAULT->value)->update([
                'is_default'=>UserAccountDefaultConst::NOT_DEFAULT->value
            ]);
    }


    /**
     * 通过多个 id 获取账号信息
     * @param int $uid
     * @param array $ids
     * @return UsersAccount[]|\Hyperf\Collection\Collection|\Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection|\Hyperf\Database\Query\Builder[]
     */
    public function getAccountByIds(int $uid,array $ids)
    {
        return UsersAccount::query()
            ->where('uid','=',$uid)
            ->whereIn('id',$ids)
            ->where('is_del','=',SysStatusConst::NORMAL->value)
            ->get();

    }
}